package com.learn.mq.controller;

import com.learn.mq.beans.Person;
import com.learn.mq.util.MessageConverter;
import lombok.extern.slf4j.Slf4j;

import org.apache.rocketmq.client.producer.*;
import org.apache.rocketmq.common.message.Message;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * ClassName:SendMsgController
 * Package:com.learn.mq.controller
 * Description:
 *
 * @Author:@郭先森
 * @Create:2025/7/30 - 7:29
 * @Version:v1.0
 */
@RestController
@Slf4j
public class SendMsgController {
    @Autowired
    private DefaultMQProducer learnClientProducer;
    @Autowired
    private TransactionMQProducer transactionMQProducer;

    //普通消息同步发送
    @GetMapping("/sendMsg1")
    public boolean sengMsg() throws Exception {
        Person person = new Person(1,"张三",new Date(),'男',"18888888888");
        Message message = MessageConverter.toRocketMessage("GXS-LEARN-CLIENT","GXS",person);
        SendResult result = learnClientProducer.send(message);
        if (result!=null&&result.getSendStatus()== SendStatus.SEND_OK){
            log.info("sendMsg1发送了消息: "+message);
            return true;
        }
        return false;
    }

    //异步发送
    @GetMapping("/sendMsg2")
    public int sengMsg2() throws Exception{
        Person person = new Person(2,"李四",new Date(),'女',"166666666668");
        Message message = MessageConverter.toRocketMessage("GXS-LEARN-CLIENT","GXS",person);
         learnClientProducer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("sengMsg2的异步消息已经成功发送："+message);
            }

            @Override
            public void onException(Throwable throwable) {
                log.info("sengMsg2的异步消息发送失败："+message);
            }
        });
         return 2;
    }

    @GetMapping("/sendMsg3")
    public int sengMsg3() throws Exception{
        Person person = new Person(3,"王五",new Date(),'男',"988756");
        Message message = MessageConverter.toRocketMessage("GXS-LEARN-CLIENT","GXS",person);
        learnClientProducer.sendOneway(message);
        log.info("发送了一个单向消息: "+message);
        return 3;
    }

    //发送延时消息
    @GetMapping("/sendMsg4")
    public boolean sengMsg4() throws Exception{
        Person person = new Person(4,"赵六",new Date(),'女',"78944545645611");
        Message message = MessageConverter.toRocketMessage("GXS-LEARN-CLIENT","GXS",person);
        message.setDelayTimeLevel(3);
        SendResult sendResult = learnClientProducer.send(message);
        log.info("sengMsg4发送了一个延时消息，延时等级为3（10s）: "+message);
        if(sendResult.getSendStatus()==SendStatus.SEND_OK){
            return true;
        }
        return false;
    }

    @GetMapping("/sendMsg5")
    public boolean sengMsg5() throws Exception{
        Person person = new Person(5,"钱老七",new Date(),'男',"123456789");
        Message message = MessageConverter.toRocketMessage("GXS-LEARN-CLIENT-TRANSACTION","GXS-TRANSACTION",person);
        SendResult result = transactionMQProducer.sendMessageInTransaction(message,person);
        if(result.getSendStatus()==SendStatus.SEND_OK){
            log.info("sengMsg5发送了一个事务消息，消息内容为: "+message);
            return true;
        }
        return false;
    }
}
